using System.IO;
using System.Xml.Serialization;

namespace ClientTools.Client
{
    public sealed class Globals
    {
        private static string _appPath;
        public static string AppPath 
        {
            get { return _appPath; }
            set { _appPath = value; }
        }

        private static Library.Settings.ClientTools _appSettings;

        public static Library.Settings.ClientTools AppSettings
        {
            get 
            {
                if (_appSettings == null) { 
                    var path = Path.Combine(_appPath, "ClientTools.config");
                    if (File.Exists(path))
                    {
                        var ser = new XmlSerializer(typeof(Library.Settings.ClientTools));
                        using (var fs = new FileStream(path, FileMode.Open))
                        {
                            _appSettings = (Library.Settings.ClientTools)ser.Deserialize(fs);
                            fs.Close();
                        }
                    } 
                    else
                    {
                        _appSettings = new Library.Settings.ClientTools();
                        _appSettings.AddNewProfile();
                    }
                }
                return _appSettings; 
            }
        }

        public static void SaveSettings()
        {
            var path = Path.Combine(_appPath, "ClientTools.config");
            using (var fs = new FileStream(path, FileMode.Create))
            {
                var ser = new XmlSerializer(typeof(Library.Settings.ClientTools));
                ser.Serialize(fs, AppSettings);
                fs.Close();
            }
        }

    }
}
